/** 专题页面 */
export function usePage(queryData: any, run?: Function) {
  /** 数量统计 */
  const totalNum = ref(0);
  /** 表格数据 */
  const tableData = ref([]);

  const isPageChange = ref(false);
  /** 换页 */
  const handleCurrentChange = (val: number) => {
    queryData.pageNum = val;
    isPageChange.value = true;
  };

  /** 排序 */
  const sortChange = (event: any) => {
    const { column, order } = event;
    const { sortBy, field } = column;
    queryData.sortRule = order ? `${sortBy || field}:${order}` : '';
  };

  /** 检索 */
  const searchTxt = ref(queryData.searchTxt);
  const handlerInput = () => {
    queryData.searchTxt = searchTxt.value;
  };

  watch(
    () => queryData,
    (val) => {
      if (!isPageChange.value) {
        queryData.pageNum = 1;
      } else {
        isPageChange.value = false;
      }
      queryData.searchTxt = searchTxt.value;

      run && run(val);
    },
    { deep: true }
  );

  return {
    totalNum,
    tableData,
    searchTxt,
    handlerInput,
    isPageChange,
    handleCurrentChange,
    sortChange
  };
}
